#include <jni.h>
#include <string>
#include <jni.h>
#include <string>

#include <android/bitmap.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>
#include <android/log.h>
#define TAG "david"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__)
ANativeWindow *nativeWindow = NULL;
ANativeWindow_Buffer *buffer = NULL;

#include <SkBitmap.h>
#include <include/core/SkFont.h>
#include "include/core/SkCanvas.h"
#include <include/core/SkPaint.h>
#include <include/core/SkRect.h>
#include <include/core/SkColor.h>
#include <include/core/SkGraphics.h>
#include <android/bitmap.h>
#include <android/native_window.h>
#include <android/native_window_jni.h>

#include <android/log.h>
#include <include/private/SkColorData.h>
#include <include/core/SkSurface.h>

SkCanvas *skCanvas = NULL;


extern "C"
JNIEXPORT void JNICALL
Java_com_maniu_fluttermaniu_FlutterJNI_init(JNIEnv *env, jobject thiz, jobject surface,
                                            jint width, jint height) {

    nativeWindow=ANativeWindow_fromSurface(env, surface);
//nativeWindow        width  hei
    ANativeWindow_setBuffersGeometry(nativeWindow,  width, height, WINDOW_FORMAT_RGBA_8888);
    buffer = new ANativeWindow_Buffer();
    //得到一个缓冲区 对象
    ANativeWindow_lock(nativeWindow, buffer, 0);

//    skia 引擎 渲染 surfaceview

    SkBitmap skbitmap;
    SkImageInfo image_info = SkImageInfo::MakeS32(width,height,
                                                  SkAlphaType::kPremul_SkAlphaType);

    int size = buffer->stride * 4;
    skbitmap.setInfo(image_info,size);

    skbitmap.setPixels(buffer->bits);
//    buffer->bits    根源      操作    显示屏幕
    SkRect rect;
    rect.set(SkIRect::MakeWH(width,height));

    SkPaint paint;
    paint.setColor(SK_ColorYELLOW);
//skia引擎
    skCanvas= new SkCanvas(skbitmap);
    skCanvas->drawRect(rect, paint);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_maniu_fluttermaniu_FlutterJNI_drawText(JNIEnv *env, jobject thiz, jstring data_,
                                                jint left, jint top) {
    const char *data = env->GetStringUTFChars(data_, 0);
    SkFont skfont(SkTypeface::MakeDefault(),48);
    SkPaint paint2;
    paint2.setColor(SK_ColorBLACK);
    skCanvas->drawString(data,left,top,skfont,paint2);
    env->ReleaseStringUTFChars(data_, data);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_maniu_fluttermaniu_FlutterJNI_flush(JNIEnv *env, jobject thiz) {
    // TODO: implement flush()
    ANativeWindow_unlockAndPost(nativeWindow);
}


extern "C"
JNIEXPORT void JNICALL
Java_com_maniu_fluttermaniu_FlutterJNI_drawBitmap(JNIEnv *env, jobject thiz, jobject bitmap,
jint left, jint top) {

AndroidBitmapInfo info;
//width   heigth
AndroidBitmap_getInfo(env, bitmap, &info);
//bitmap   缓冲区
    int *pixels=NULL;//缓冲区  代码    skbitmap ----》    缓冲区  取到  赋值 给pixels
    AndroidBitmap_lockPixels(env, bitmap, reinterpret_cast<void **>(&pixels));

//pixels   bitmap像素
    SkBitmap bitmap2;

    SkImageInfo image_info2 = SkImageInfo::MakeS32(info.width,info.height,SkAlphaType::kPremul_SkAlphaType);
    bitmap2.setPixels(pixels);
    skCanvas->drawBitmap(bitmap2, left, top, NULL);
}